package com.jsbj.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.tools.RequestHeaderUtils;
import com.jsbj.tools.StringUtils;
import com.jsbj.user.entities.SysRoleOrg;
import com.jsbj.user.mapper.SysRoleOrgMapper;
import com.jsbj.user.service.ISysRoleOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色-标段信息表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-03-07
 */
@Service
public class SysRoleOrgServiceImpl extends ServiceImpl<SysRoleOrgMapper, SysRoleOrg> implements ISysRoleOrgService {

    @Autowired
    private HttpServletRequest request;

    @Override
    public Integer add(String inParam) {
        String userId = RequestHeaderUtils.getUserId(request);
        JSONObject object = JSONObject.parseObject(inParam);
        String orgIds = object.getString("orgIds");
        String roleId = object.getString("roleId");
        // 先删除该角色的配置
        LambdaUpdateWrapper<SysRoleOrg> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysRoleOrg::getDisabled, 0);
        updateWrapper.eq(SysRoleOrg::getRoleId, roleId);

        SysRoleOrg sysRoleOrgUp = new SysRoleOrg();
        sysRoleOrgUp.setDisabled(1);
        sysRoleOrgUp.setUpdateId(userId);
        sysRoleOrgUp.setUpdateTime(new Date());
        baseMapper.update(sysRoleOrgUp, updateWrapper);

        if (StringUtils.isNotEmpty(orgIds)) {
            List<String> orgIdList = Arrays.asList(orgIds.split(","));
            for (String orgId : orgIdList) {
                SysRoleOrg sysRoleOrg = new SysRoleOrg();
                sysRoleOrg.setOrgId(orgId);
                sysRoleOrg.setRoleId(roleId);
                sysRoleOrg.setCreaterId(userId);
                sysRoleOrg.setCreateTime(new Date());
                sysRoleOrg.setUpdateId(userId);
                sysRoleOrg.setUpdateTime(new Date());
                baseMapper.insert(sysRoleOrg);
            }
        }
//        String ids = object.getString("ids");
//        if (StringUtils.isNotEmpty(ids)) {
//            List<String> notChooseIds = Arrays.asList(ids.split(","));
//            for (String id : notChooseIds) {
//                SysRoleOrg sysRoleOrg = baseMapper.selectById(id);
//                sysRoleOrg.setDisabled(1);
//                sysRoleOrg.setUpdateId(userId);
//                sysRoleOrg.setUpdateTime(new Date());
//                baseMapper.updateById(sysRoleOrg);
//            }
//        }
        return 1;
    }

    @Override
    public List<SysRoleOrg> detail(String id) {
        LambdaQueryWrapper<SysRoleOrg> qw = new LambdaQueryWrapper<>();
        qw.eq(SysRoleOrg::getRoleId, id);
        qw.eq(SysRoleOrg::getDisabled, 0);
        List<SysRoleOrg> sysRoleOrgs = baseMapper.selectListAndName(id);
        return sysRoleOrgs;
    }

    @Override
    public void insertData(String orgId, String roleId, String userId) {
        LambdaQueryWrapper<SysRoleOrg> qw = new LambdaQueryWrapper<>();
        qw.eq(SysRoleOrg::getRoleId, roleId);
        qw.eq(SysRoleOrg::getOrgId, orgId);
        qw.eq(SysRoleOrg::getDisabled, 0);
        List<SysRoleOrg> sysRoleOrgs = baseMapper.selectList(qw);
        if (CollectionUtils.isEmpty(sysRoleOrgs)) {
            SysRoleOrg sysRoleOrg = new SysRoleOrg();
            sysRoleOrg.setOrgId(orgId);
            sysRoleOrg.setRoleId(roleId);
            sysRoleOrg.setCreaterId(userId);
            sysRoleOrg.setCreateTime(new Date());
            sysRoleOrg.setUpdateId(userId);
            sysRoleOrg.setUpdateTime(new Date());
            baseMapper.insert(sysRoleOrg);
        }
    }
}
